'use strict';

angular.module('afasApp')
  .controller('AmountTypesCtrl', ['$scope', 'Services', '$state', function ($scope, Services, $state) {

    $scope.amountTypes = [];
    $scope.amountTypeNew = {};

    function applyRemoteData( newData ) {
      $scope.amountTypes = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.AMOUNTTYPE_URL)
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addAmountType = function () {
      Services.add($scope.AMOUNTTYPE_URL, $scope.amountTypeNew)
      .then(function(results){
        $scope.amountTypeNew = {};
        console.log(results);
        $state.go( 'amounttypes.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
      // AmountTypes.addAmountType($scope.amountTypeNew);
    };

    $scope.deleteAmountType = function (amountType) {
      Services.delete($scope.AMOUNTTYPE_URL, amountType)
      .then(function(results){
        console.log(results);
        loadRemoteData();
      });
      // AmountTypes.deleteAmountType(amountType);
    };
  }]).controller('AmountTypeCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.currentAmountType = {};
    $scope.currentID = $stateParams.id;

    Services.getItem($scope.AMOUNTTYPE_URL, $stateParams.id)
    .then(function(results){
      $scope.currentAmountType = results.data;
    });
    // $scope.currentAmountType =AmountTypes.getAmountType($stateParams.id);

    $scope.updateAmountType = function () {
      Services.update($scope.AMOUNTTYPE_URL, $scope.currentAmountType, $scope.currentID)
      .then(function(results){
        console.log(results);
        $state.go( 'amounttypes.show', {id: $scope.currentID} );
      });
      // amountTypes.updateamountType($scope.currentamountType, $scope.currentID);
    };
  }]);